﻿using NHibernate;
using NLog;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Susha.Models.Helpers
{
    public static class CategoryRepository
    {
        private static Logger _logger = LogManager.GetCurrentClassLogger();

        public static Category Save(Category category)
        {
            using (ISession session = NHibernateHelper.OpenSession())
            {
                using (ITransaction transaction = session.BeginTransaction())
                {
                    session.Save(category);
                    transaction.Commit();

                    _logger.Info(String.Format("Category {0} has been created by {1};", 
                                                    category.Name, 
                                                    HttpContext.Current.User.Identity.Name));
                    return category;
                }
            }
        }

        public static String DeleteCategory(Int32 categoryId)
        {
            String resultError = String.Empty;
            using (ISession session = NHibernateHelper.OpenSession())
            {
                Category category = session.QueryOver<Category>().Where(x => x.Id == categoryId).SingleOrDefault<Category>();
                if (category != null && category.Sushi != null)
                {
                    using (ITransaction transaction = session.BeginTransaction())
                    {
                        foreach (Sushi s in category.Sushi)
                        {
                            session.Delete(s);
                            CommonRepository.DeletePhoto(s.SmallPhoto);
                        }
                        session.Delete(category);
                        transaction.Commit();
                    }
                }
                else
                {
                    resultError = "Не могу найти категорию"; //Can't find category
                }

            }
            return resultError;
        }

        public static IList<Category> GetAllCategories()
        {
            using (ISession session = NHibernateHelper.OpenSession())
            {
                return
                    session.QueryOver<Category>().List<Category>()
                    ?? new List<Category>();
            }
        }

        public static Category GetCategoryById(Int32 id)
        {
            using (ISession session = NHibernateHelper.OpenSession())
            {
                Category category = session.QueryOver<Category>().Where(x => x.Id == id).SingleOrDefault();
                if (category == null)
                {
                    _logger.Error(String.Format("GetCategoryById({0}): null result", id));
                }
                return category;
            }
        }

        public static Boolean ChangeCategoryName(Int32 id, String newName)
        {
            Boolean result = false;
            using (ISession session = NHibernateHelper.OpenSession())
            {
                Category category = session.QueryOver<Category>().Where(x => x.Id == id).SingleOrDefault();
                if (category != null)
                {
                    String oldName = category.Name;
                    category.Name = newName;
                    using (ITransaction tranaction = session.BeginTransaction())
                    {
                        session.SaveOrUpdate(category);
                        tranaction.Commit();

                        _logger.Info("Category was renamed {0} => {1} by {2}", 
                                        oldName, 
                                        newName, 
                                        HttpContext.Current.User.Identity.Name);
                        result = true;
                    }
                }
                else
                {
                    _logger.Error(String.Format("ChangeCategoryName({0}, {1}): category is null", id, newName));
                }
            }
            return result;
        }
    }
}